package com.uenpay.oem.data.modelImpl;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.qiyunxin.android.http.listener.OnHttpRequestTextListener;
import com.qiyunxin.android.http.net.HttpInvokeItem;
import com.qiyunxin.android.http.net.HttpRequestAsyncTaskQueue;
import com.qiyunxin.android.http.net.HttpRequestType;
import com.qiyunxin.android.http.net.HttpTextAsyncTask;
import com.uenpay.im.entities.QYXUserEntity;
import com.uenpay.im.invokeitems.person.GetPersonInfoInvokeItem;
import com.uenpay.oem.MyApplication;
import com.uenpay.oem.common.TransCodeConstant;
import com.uenpay.oem.data.bean.Request;
import com.uenpay.oem.data.bean.UserAuthInfo;
import com.uenpay.oem.data.bean.UserIdCardInfo;
import com.uenpay.oem.data.model.IUserAuthModel;
import com.uenpay.oem.net.RetrofitHttpService;
import com.uenpay.util.net.http.BaseRequest;
import com.uenpay.util.net.http.IHttpManager;
import com.uenpay.util.net.listener.HttpOnNextListener;
import com.uenpay.util.net.listener.HttpResultListener;
import com.uenpay.util.net.retrofit.HttpManager;
import com.uenpay.util.net.retrofit.RetrofitRequest;
import com.uenpay.util.net.retrofit.UenException;

import java.util.HashMap;
import java.util.Map;

import rx.Observable;

/**
 * Author: liulou
 * Date: 2017/6/12.
 * desc: 用户认证相关
 */

public class UserAuthModel implements IUserAuthModel {

    private IHttpManager manager = null;
    private BaseRequest authImageUploadReq = null;
    private BaseRequest userRealNameAuthReq = null;
    private BaseRequest userCreditVerifyReq = null;

    public UserAuthModel() {
        this.manager = HttpManager.getInstance(RetrofitHttpService.class);
    }

    @Override
    public void userRealNameImageUpload(final String data, HttpOnNextListener<UserIdCardInfo> listener) {
        if (manager == null) {
            return;
        }
        authImageUploadReq = new RetrofitRequest(listener) {
            @Override
            public Observable getObservable(Object methods) {
                Request request = new Request(TransCodeConstant.CODE_USER_REAL_NAME_IMAGE_UPLOAD);
                Map<String, String> body = new HashMap<>();
                body.put("photoPositiveImage", data);
                request.setBody(body);
                return ((RetrofitHttpService) methods).userRealNameImageUpload(request);
            }
        };
        manager.doHttpDeal(authImageUploadReq);
    }

    @Override
    public void userRealNameAuth(final QYXUserEntity userInfo, final UserIdCardInfo info, HttpResultListener listener) {
        if (manager == null) {
            return;
        }
        userRealNameAuthReq = new RetrofitRequest(listener) {
            @Override
            public Observable getObservable(Object methods) {
                Request request = new Request(TransCodeConstant.CODE_USER_REAL_NAME_AUTH);
                Map<String, String> body = new HashMap<>();
                body.put("cerNo", info.getCerNo());
                body.put("cerName", info.getCerName());
                body.put("mobile", MyApplication.getUserMobilePhone());
                body.put("nickname", userInfo.nickname);
                body.put("sex", userInfo.sex + "");
                body.put("email", userInfo.email);
                body.put("area", userInfo.area);
                request.setBody(body);
                return ((RetrofitHttpService) methods).userRealNameAuth(request);
            }
        };
        manager.doHttpDeal(userRealNameAuthReq);
    }

    @Override
    public void userCreditVerify(HttpOnNextListener<UserAuthInfo> listener) {
        if (manager == null) {
            return;
        }
        userCreditVerifyReq = new RetrofitRequest(listener) {
            @Override
            public Observable getObservable(Object methods) {
                Request request = new Request(TransCodeConstant.CODE_USER_CREDIT_VERIFY);
                return ((RetrofitHttpService) methods).userCreditVerify(request);
            }
        };
        manager.doHttpDeal(userCreditVerifyReq);
    }

    @Override
    public void userInfoQuery(final HttpOnNextListener<QYXUserEntity> listener) {
        GetPersonInfoInvokeItem item = new GetPersonInfoInvokeItem();
        HttpRequestAsyncTaskQueue
                .getInstance()
                .AddTask(
                        new HttpTextAsyncTask(item)
                                .SetRequestType(HttpRequestType.CACHE_FIRST_AND_SERVER),
                        new OnHttpRequestTextListener() {

                            @Override
                            public void OnFail(boolean isCache, String reason) {
                                listener.onError(new UenException(reason));
                            }

                            @Override
                            public void OnProgress(long total_bytes,
                                                   long downloaded_bytes) {
                            }

                            @Override
                            public void OnSuccess(HttpInvokeItem item,
                                                  boolean isCache, String cacheFilePath) {
                                GetPersonInfoInvokeItem.GetPersonInfoInvokeItemResult result = ((GetPersonInfoInvokeItem) item)
                                        .getOutput();
                                if (result != null
                                        && result.status == HttpInvokeResultCode.RESULT_OK) {
                                    listener.onNext(result.user);
                                }
                            }
                        });
    }
}
